package com.project.les.view.adapters;

import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.project.les.R;
import com.project.les.ViewFeriadoActivity;
import com.project.les.view.elements.FeriadoViewElement;

public class FeriadoListAdapter extends BaseAdapter {
	private Context context;
	private List<FeriadoViewElement> elementList;
	private List<FeriadoViewElement> feriadoes;

	public FeriadoListAdapter(Context context,
			List<FeriadoViewElement> elementList) {
		this.context = context;
		this.elementList = elementList;
	}

	public FeriadoListAdapter(Context context,
			List<FeriadoViewElement> elementList,
			List<FeriadoViewElement> feriadoes) {
		this.context = context;
		this.elementList = elementList;
		this.feriadoes = feriadoes;
	}

	public int getCount() {
		return elementList.size();
	}

	public Object getItem(int position) {
		return elementList.get(position);
	}

	public long getItemId(int position) {
		return position;
	}

	public View getView(int position, View convertView, ViewGroup parent) {

		// Recupera o estado da posição atual
		FeriadoViewElement feriadoViewElement = elementList.get(position);

		// Cria uma instância do layout XML para os objetos correspondentes na
		// View
		LayoutInflater inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View view = inflater.inflate(R.layout.feriado_list_item, null);

		// Primeiro elemento da lista com layout diferente
		if (position == 0) {
			// Data
			TextView date = (TextView) view.findViewById(R.id.date);
			date.setTextColor(Color.parseColor("#000000"));
			date.setText(feriadoViewElement.getDay() + " / "
					+ feriadoViewElement.getMonth() + " / "
					+ feriadoViewElement.getYear());
			date.setTypeface(Typeface.DEFAULT_BOLD);
			//date.setBackgroundColor(Color.parseColor("#98AFC7"));

			// Nome
			TextView name = (TextView) view.findViewById(R.id.name);
			name.setText(feriadoViewElement.getName());
			name.setTextColor(Color.parseColor("#000000"));
			name.setTypeface(Typeface.DEFAULT_BOLD);
			//name.setBackgroundColor(Color.parseColor("#98AFC7"));
			
			if(ehFeriadao(feriadoViewElement)) {
				date.setTextColor(Color.parseColor("#FF0000"));
				name.setTextColor(Color.parseColor("#FF0000"));
			}
		}

		else {
			// Data
			TextView date = (TextView) view.findViewById(R.id.date);
			date.setTextColor(Color.parseColor("#000000"));
			date.setText(feriadoViewElement.getDay() + " / "
					+ feriadoViewElement.getMonth() + " / "
					+ feriadoViewElement.getYear());

			// Nome
			TextView name = (TextView) view.findViewById(R.id.name);
			name.setText(feriadoViewElement.getName());
			name.setTextColor(Color.parseColor("#000000"));
			
			if(ehFeriadao(feriadoViewElement)) {
				date.setTextColor(Color.parseColor("#FF0000"));
				name.setTextColor(Color.parseColor("#FF0000"));
				name.setTypeface(Typeface.DEFAULT_BOLD);
				date.setTypeface(Typeface.DEFAULT_BOLD);
			}
				
		}

		TextView regId = (TextView) view.findViewById(R.id.tvRegId);
		regId.setText(String.valueOf(feriadoViewElement.getId()));

		view.setOnTouchListener(new OnTouchListener() {

			// @Override
			public boolean onTouch(View v, MotionEvent event) {
				if (event.getAction() == MotionEvent.ACTION_UP) {
					Intent i = new Intent(v.getContext(),
							ViewFeriadoActivity.class);

					TextView tvRegId = (TextView) v.findViewById(R.id.tvRegId);
					Integer regId = Integer.valueOf(tvRegId.getText()
							.toString());

					i.putExtra("id", regId);
					v.getContext().startActivity(i);
				}
				return true;
			}
		});

		return view;
	}
	
	private boolean ehFeriadao(FeriadoViewElement feriadoViewElement) {
		if(this.feriadoes == null) {
			return false;
		}
		else if(this.feriadoes.contains((FeriadoViewElement)feriadoViewElement)) {
			return true;
		}
		return false;
		
		
		
	}
}